package ynu.edu.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ynu.edu.dto.ResponseResult;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    /**
     * 处理资源未找到异常
     * @param ex 异常
     * @return 响应结果
     */
    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseResult<String> handleResourceNotFoundException(ResourceNotFoundException ex) {
        return ResponseResult.error(404, ex.getMessage());
    }
    
    /**
     * 处理认证异常
     * @param ex 异常
     * @return 响应结果
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseResult<String> handleAuthenticationException(AuthenticationException ex) {
        return ResponseResult.error(401, ex.getMessage());
    }
    
    /**
     * 处理通用异常
     * @param ex 异常
     * @return 响应结果
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult<String> handleGlobalException(Exception ex) {
        return ResponseResult.error("服务器内部错误: " + ex.getMessage());
    }
} 